package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.StrategyComment;
import cn.wolfcode.trip.base.domain.Tag;
import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.mapper.CommentMapper;
import cn.wolfcode.trip.base.mapper.StrategyCommentMapper;
import cn.wolfcode.trip.base.mapper.TagMapper;
import cn.wolfcode.trip.base.query.StrategyCommentQueryObject;
import cn.wolfcode.trip.base.service.IStrategyCommentService;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

@Service
public class StrategyCommentServiceImpl implements IStrategyCommentService {
    @Autowired
    private StrategyCommentMapper strategyCommentMapper;
    @Autowired
    private TagMapper tagMapper;
    @Autowired
    private CommentMapper commentMapper;

    @Override
    public void saveOrUpdate(StrategyComment strategyComment) {
        if (strategyComment.getId() == null) {
            strategyCommentMapper.insert(strategyComment);
        } else {
            strategyCommentMapper.updateByPrimaryKey(strategyComment);
        }
    }

    @Override
    public PageInfo query(StrategyCommentQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<StrategyComment> list = strategyCommentMapper.selectForList(qo);
        qo.setType(1);
        List<Map> maps = commentMapper.selectStrategyCommentForCount(qo);
        for (StrategyComment strategyComment : list) {
            Long id = strategyComment.getId();
            for (Map map : maps) {
                if(map.get("cid")==id){
                    strategyComment.setStrategyCommentNum((Long) map.get("count"));
                }
            }
        }
        return new PageInfo(list);
    }

    @Override
    public void save(StrategyComment strategyComment, String[] tags) {
        //设置评论人
        strategyComment.setAuthor(UserContext.getCurrentUser());
        strategyCommentMapper.insert(strategyComment);

        //保存标签
        for (String tagStr : tags) {
            if (StringUtils.hasLength(tagStr)) {

                Tag tag = new Tag();
                tag.setName(tagStr);
                tagMapper.insert(tag);

                //关联中间表
                strategyCommentMapper.insertRelation(strategyComment.getId(), tag.getId());
            }
        }
    }

    @Override
    public StrategyComment getCommentById(Long commentId) {
        return strategyCommentMapper.selectByPrimaryKey(commentId);
    }

    @Override
    public List<StrategyComment> allComments(StrategyCommentQueryObject qo) {
        return strategyCommentMapper.allComments(qo);
    }

    @Override
    public PageInfo selectCommentsByAuthor(StrategyCommentQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<StrategyComment> list = strategyCommentMapper.selectCommentsByAuthor(qo);
        return new PageInfo(list);
    }

   /* @Override
    public List<StrategyComment> selectByStrategyId(Long strategyId) {
        return strategyCommentMapper.selectByStrategyId(strategyId);
    }*/
}
